import time
class KeyLib_ui(object):
    def __init__(self,driver):
        self.driver=driver
    def get_element(self,*args):
        '''
        查找元素的关键字方法
        :param args: 定位方法=元素
        :return: 布尔值
        '''
        try:
            how,what=args[0].split('=',1)[0],args[0].split('=',1)[1]
            if '_' in how:
                how=how.replace('_',' ')
            element=self.driver.find_element(how,what)
            if element:
                return element
        except Exception as e:
            print(f'{args[0]}未找到或者错误----------',e)
    def open(self,*args):
        '''
        打开url网址
        :param args: url
        :return: 布尔值
        '''
        try:
            self.driver.get(args[0])
            return True
        except Exception as e:
            print(f'open:--------',e)
            return False
    def input(self,*args):
        '''
        对某个元素做输入操作得关键字
        :param args: 元素，输入得内容
        :return: 布尔值
        '''
        element=self.get_element(args[0])
        try:
            element.send_keys(args[1].split('=')[1])
            return True
        except Exception as e:
            print(f'input_text:{args[0]}-----------',e)
            return False
    def click(self,*args):
        '''
        点击某个元素关键字
        :param args: 元素
        :return: 布尔值
        '''
        try:
            self.element = self.get_element(args[0]).click()
            return True
        except Exception as e:
            print(f'click:{args[0]}------------------',e)
            return False
    def wait(self,*args):
        '''
        强制等待
        :param args: 等待得时间
        :return: 布尔值
        '''
        try:
            time.sleep(int(args[0].split('=')[1]))
            return True
        except Exception as e:
            print(f'wait:错误',e)
            return False
    def should_contain(self,*args):
        '''
        断言判断是否网页里得源码是否包含预取
        :param args: 是否被包含的内容
        :return: 布尔值
        '''
        if args[0] in self.driver.page_source:
                return True
        else:
            print(f'should_contain:{args[0]}未能被包含或者其他错误')
            return False
    def should_equal(self,*args):
        '''
        断言判断是否预期和响应body相等
        :param args: 是否被相等的内容
        :return: 布尔值
        '''
        try:
            text = self.get_element(args[0]).text
            expect = args[1].split('=')[1]
            if text==expect:
                    return True
            else:
                print(f'should_queal:{expect}未能相等或其他错误')
                return False
        except Exception as e:
            print('should_equal:遇到报错---------',e)
            return False
